package com.chinemit.cui.components.button
{
	import com.chinemit.cui.components.bitmap.BitmapScale9Grid;
	import com.chinemit.cui.components.core.CUIComponent;
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	public class ImageButton9Grid extends CUIComponent
	{
		private var imgContainer:BitmapScale9Grid;
		private var overImg:Bitmap;
		private var downImg:Bitmap;
		private var curImg:Bitmap;
		
		private var btnWidth:Number;
		private var btnHeight:Number;
		
		private var gridTop:Number;
		private var gridBottom:Number;
		private var gridLeft:Number;
		private var gridRight:Number;
		
		private var lable:TextField;
		private var labeTM:TextFormat;
		
		private var _text:String;
		
		private var _txtFilters:Array; 
		
		private var  glow:GlowFilter;
		
		private var offsetY:Number = 2;
		
		public static const COLOR_BLUE:uint = 0x294a7f;
		
		public static const COLOR_YELLOW:uint = 0x774e0e;
		
		public static const COLOR_GREEN:uint = 0x006600;
		
		public function ImageButton9Grid(_txt:String,_img:Bitmap,_overImg:Bitmap,_downImg:Bitmap,
										 _w:Number,_h:Number,_glowColor:uint=0x774e0e,left:Number=10,right:Number=25,
										 top:Number=10,bottom:Number=25,_buttonMode:Boolean = true,tf:TextFormat=null)
		{
			super();
			buttonMode = _buttonMode;
			this.mouseChildren = false;
			//
			curImg = _img;
			overImg = _overImg;
			downImg = _downImg; 
			//
			this.btnWidth = _w;
			this.btnHeight = _h;
			//
			this.gridBottom = bottom;
			this.gridTop =  top;
			this.gridLeft = left;
			this.gridRight = right;
			//
			this.text = _txt;
			labeTM = tf;
			glow  =  new GlowFilter(_glowColor,1,6,6,20);
			this.lable.filters = new Array(glow);
			
		}
		
		/**
		 * 字体滤镜
		 * @return 
		 * 
		 */		
		public function get txtFilters():Array
		{
			return _txtFilters;
		}
			
		public function set txtFilters(value:Array):void
		{
			_txtFilters = value;
			if(_txtFilters){
				this.lable.filters = _txtFilters;
			}else{
				this.lable.filters = new Array(glow);
			}
		}
 
		public function get text():String
		{
			return _text;
		}

		public function set text(value:String):void
		{
			_text = value;
			if(_text){
				if(!lable){
					lable = new TextField();
					lable.selectable = false;
				}
				lable.text = _text;
				lable.width = this.btnWidth;
				lable.height = lable.textHeight+5;
				
				if(!labeTM){
					
					labeTM = new TextFormat("Wild Words",22,0xffffff,"Bold");
					labeTM.leading = 2;
					labeTM.align = TextFormatAlign.CENTER;
					lable.embedFonts = true;
					lable.antiAliasType = AntiAliasType.ADVANCED;
					lable.setTextFormat(labeTM);
					//
					lable.y = (this.btnHeight - lable.textHeight)*0.5;
				}
			}
		}

		override protected function into():void{
			super.into();
			updateImg(curImg);
			this.addEventListener(MouseEvent.MOUSE_OVER,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
			
			this.addChild(lable);
		}
		
		override protected function out():void{
			return;
			super.out();
			//dispose();
		}
		
		/**
		 * 更新图像
		 * @param img
		 * 
		 */		
		private function updateImg(img:Bitmap):void{
			
			if(imgContainer == null){
				imgContainer = new BitmapScale9Grid(img,gridTop,gridBottom,gridLeft,gridRight);
				imgContainer.width = btnWidth;
				imgContainer.height = btnHeight;
				
				this.addChild(imgContainer);
			}else{
				imgContainer.reset(img,gridTop,gridBottom,gridLeft,gridRight)
				imgContainer.width = btnWidth;
				imgContainer.height = btnHeight;
			}
			
//			if(curImg!=null){
//				if(contains(curImg)){
//					removeChild(curImg);	
//				}
//			}
//			addChild(img);
//			curImg = img;
			
		}
		
		/**
		 * 
		 * @param e
		 *  
		 */		
		private function mouseEventHandler(e:MouseEvent):void{
			
			lable.y = (this.btnHeight - lable.textHeight)*0.5;
			switch(e.type){
				case MouseEvent.MOUSE_OVER:
					updateImg(overImg);
					break;
				case MouseEvent.MOUSE_OUT:
					updateImg(curImg);
					break;
				case MouseEvent.MOUSE_UP:
					updateImg(curImg);
					break;
				case MouseEvent.MOUSE_DOWN:
					updateImg(downImg);
					lable.y+=offsetY;
					break;
			}
		}
		
		/**
		 * 销毁
		 * 
		 */		
		private function dispose():void{
			overImg.bitmapData.dispose();
			downImg.bitmapData.dispose();
//			imgContainer.bitmapData.dispose();
			overImg = null;
			downImg = null;
			imgContainer = null;
			
			this.removeEventListener(MouseEvent.MOUSE_OVER,mouseEventHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT,mouseEventHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
		}
	}
}